﻿@{
    var photoId = UrlData[0].AsInt();

    var db = Database.Open("PhotoGallery");

    var photo = db.QuerySingle("SELECT * FROM Photos WHERE Id = @0", photoId);
    if (photo == null) {
        Response.SetStatus(HttpStatusCode.NotFound);
        return;        
    }

    Page.Title = "Photo - " + photo.FileTitle;

    string description = photo.description;

    if (IsPost) {
        var newComment = Request["newComment"].Trim();
        if (newComment.IsEmpty()) {
            Response.Redirect(Href("View", photoId));
        }

        db.Execute("INSERT INTO Comments (PhotoId, CommentDate, UserId, CommentText) VALUES (@0, @1, @2, @3)",
            photoId, DateTime.Now, WebSecurity.CurrentUserId, newComment);

        Response.Redirect(Href("View",photoId));
        return;
    }

    // Found a match - so show its info
    var user = db.QuerySingle("SELECT * FROM UserProfiles WHERE UserId = @0", photo.UserId);
    var gallery = db.QuerySingle("SELECT * FROM Galleries WHERE Id = @0", photo.GalleryId);
    var comments = db.Query(@"SELECT * FROM Comments INNER JOIN UserProfiles
                              ON Comments.UserId = UserProfiles.UserId WHERE PhotoId = @0 ORDER BY Comments.CommentDate", photo.Id);
    var tagList = db.Query("SELECT Tags_TagName FROM Photos_Tags WHERE Photos_Id = @0 ORDER BY Tags_TagName", photo.Id);
}

<div class="sidebar">
    <h1>@photo.FileTitle</h1>
    @if (user.UserId == WebSecurity.CurrentUserId) {
        <p>
            As the author, you can <a href="@Href("~/Photo/Edit", photoId)">edit</a>
            or <a href="@Href("~/Photo/Remove", photoId)">remove</a> this photo.
        </p>
    }
    <dl>
        <dt class="description">Description</dt>
        <dd class="description">
            @if (!description.IsEmpty()) {
                <text>@photo.Description</text>
            } else {
                <text>The uploader did not provide a description for this photo.</text>
            }
        </dd>
        <dt class="uploaded-by">Uploaded by</dt>
        <dd class="uploaded-by"><a href="@Href("~/User/View", user.UserId)">@user.DisplayName</a></dd>
        <dt class="upload-date">Upload date</dt>
        <dd class="upload-date">@photo.UploadDate</dd>
        <dt class="part-of-gallery">Gallery</dt>
        <dd><a href="@Href("~/Gallery/View", gallery.Id)" title="View @gallery.Name gallery">@gallery.Name</a></dd>
        <dt class="tags">Tags</dt>
        <dd class="tags">
            <ul class="tags">
            @if (tagList.Count == 0) {
                <li>This photo has no tags.</li>
            } else {
                 foreach (var t in tagList) {
                    <li class="tag"><a href="@Href("~/Tag/View", t.Tags_TagName)">@t.Tags_TagName</a></li>
                }
            }
            </ul>
            @if (WebSecurity.IsAuthenticated) {
                <a href="@Href("~/Photo/EditTags", photoId)">edit tags</a>
            }
        </dd>
    </dl>

    <p>
        <a class="download" href="@Href("~/Photo/Full", photoId)" title="Download: @(photo.FileTitle + photo.FileExtension)">Download full photo</a> (@(photo.FileSize / 1024) KB)
    </p>
</div>
<div class="main">
    <img class="large-photo" alt="@Html.AttributeEncode(photo.FileTitle)" src="@Href("~/Photo/Thumbnail", photo.Id, new { size="large" })" />

    @switch ((int)comments.Count) {
        case 0:
            <h2>Nobody has commented on this photo</h2>
            break;
        case 1:
            <h2>This photo has generated one comment.</h2>
            break;
        default:
            <h2>This photo has generated @comments.Count comments.</h2>
            break;
    }

    <ol class="comments">
        @foreach (var comment in comments) {
            <li>
                <h3 class="comment-header">
                    <a href="@Href("~/User/View", comment.UserId)" title="View @(comment.DisplayName)'s profile">@comment.DisplayName</a> commented at @comment.CommentDate:
                </h3>
                <p class="comment-body">@comment.CommentText</p>
            </li>
        }
    </ol>

    @if (WebSecurity.IsAuthenticated) {
        <form method="post" action="">
            <fieldset id="addComment">
                <legend>Post new comment</legend>
                <ol>
                    <li>
                        <label for="newComment">Comment</label>
                        <textarea id="newComment" name="newComment" title="Your comment" rows="6" cols="70"></textarea>
                    </li>
                </ol>
                <p class="form-actions">
                    <input type="submit" title="Add comment" value="Add comment" />
                </p>
            </fieldset>
        </form>
    } else {
        <p>
            Only registered user may leave comments. Please <a href="@Href("~/Account/Login")">login</a> 
            or <a href="@Href("~/Account/Register")">create an account</a> to leave a comment.
        </p>
    }
</div>